﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    class Unit
    {
        public CircleParticleEngine glowParticle;

        protected Vector2 UnitLoc;
        protected Vector2 UnitDest;
        protected Texture2D texture;
        protected float angle;
        protected string status;

        protected int unitSize = 64;

        protected int spawnTime;
        protected int createdTime;
        protected bool spawned;

        protected int radius = 30;

        public Unit(float X, float Y, List<Texture2D> particleTextures, int sT)
        {
            UnitLoc = new Vector2(-10,-10);
            UnitDest = new Vector2(X, Y);
            status = "Ready";
            glowParticle = new CircleParticleEngine(particleTextures, UnitLoc, 0.05f, 32.0f, 25);
            createdTime = 0;
            spawnTime = sT;
        }

        public float X
        {
            get { return UnitLoc.X; }
        }

        public float Y
        {
            get { return UnitLoc.Y; }
        }

        public int Radius
        {
            get { return radius; }
        }

        public string Status
        {
            get { return status; }
            set { status = value; }
        }

        public int Size
        {
            get { return unitSize; }
        }

        public virtual void goTo(float X, float Y)
        {
            if (spawned)
            {
                UnitDest = new Vector2(X, Y);
                angle = (float)Math.Atan((UnitDest.Y - UnitLoc.Y) / (UnitDest.X - UnitLoc.X));
                if (UnitLoc.X > X)
                {
                    angle += (float)Math.PI;
                }
            }
        }

        public bool Spawned
        {
            get { return spawned; }
        }

        public virtual void Update(GameTime gameTime)
        {
            if (createdTime == 0)
            {
                spawnTime = gameTime.TotalGameTime.Seconds + spawnTime;
                createdTime = gameTime.TotalGameTime.Seconds;
            }

            int timeToSpawn = spawnTime - gameTime.TotalGameTime.Seconds;

            if (timeToSpawn <= 0 && !spawned)
            {
                spawned = true;
                this.goTo(UnitDest.X, UnitDest.Y);
            }

            if (!spawned)
            {
                status = timeToSpawn.ToString();
            }
            else
            {
                float distance;
                Vector2.Distance(ref UnitDest, ref UnitLoc, out distance);
                if (distance < 2)
                {
                    UnitLoc = new Vector2(UnitDest.X, UnitDest.Y);
                    status = "Ready";
                }
                else
                {
                    status = "Moving";
                    UnitLoc += (float)1.5 * (new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
                }
                glowParticle.Update(UnitLoc);
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (spawned)
            {
                spriteBatch.Draw(texture,
                    new Rectangle(
                        (int)(UnitLoc.X) - unitSize / 2,
                        (int)(UnitLoc.Y) - unitSize / 2,
                        unitSize,
                        unitSize),
                    Color.White);
            }
        }

        public virtual void DrawSelection(SpriteBatch spriteBatch)
        {
            glowParticle.Draw(spriteBatch,new Vector2(0,0));
            /*
            spriteBatch.Draw(ResourceManager.getTexture("Glow"),
                new Rectangle(
                    (int)UnitLoc.X - cameraX - unitSize / 2,
                    (int)UnitLoc.Y - cameraY - unitSize / 2,
                    unitSize,
                    unitSize),
                Color.White);
             */
        }

        public virtual void DrawIcon(SpriteBatch spriteBatch, int x, int y)
        {
            spriteBatch.Draw(texture,
                new Rectangle(
                    x,
                    y,
                    unitSize / 2,
                    unitSize / 2),
                Color.White);
        }

        public virtual void DrawIconGlow(SpriteBatch spriteBatch, int x, int y)
        {
            spriteBatch.Draw(ResourceManager.getTexture("Glow"),
                new Rectangle(
                    x,
                    y,
                    unitSize / 2,
                    unitSize / 2),
                Color.White);
        }
    }
}
